var GlobalAllLayers;
var skyline ;
define(['crud'],function(crud){
	var mapurlIP = "http://221.214.108.194:8060/iserver/services/";
    var mapurl = mapurlIP + "3D-pingyin/rest/realspace";
    flyGlobal.mapDemUrl = mapurlIP + "3D-pingyin/rest/realspace/datas/DEM";
    flyGlobal.mapDemTmpUrl = "http://www.supermapol.com/realspace/services/3D-dixingyingxiang/rest/realspace/datas/DatasetDEM";

	var mapPan;
    var scenePan;
    var map;    
	var flyManager;
	
	function onload(Cesium,APP){
		viewer = new Cesium.Viewer('cesiumContainer',{
			// requestVertexNormals : true
		});
		var AllLayers = viewer.imageryLayers;
		scene = viewer.scene;
		skyline = new Cesium.Skyline(scene);
		var imageryLayers = viewer.imageryLayers;
        var baseLayer = imageryLayers.get(0);
        var list;
		var layerList;		
       
		try {

			var promise = scene.open(mapurl); 

			Cesium.when(promise,function(layer){ 
			console.log(layer);
				//初始化
				layers = layer;
				GlobalAllLayers = layer;
				//设置相机位置、视角，便于观察场景
				scene.camera.setView({
					destination : new Cesium.Cartesian3.fromDegrees(116.464386111111,36.30124166666,117.224091),
					orientation : {
							heading : 117.69,
							pitch : 50.300289
					} 
				}); 

                //初始化飞行
				// $("#flyControl").show();
				var routes = new Cesium.RouteCollection();
				var fpfUrl = publicDir + "/fly/loading.fpf";
				routes.fromFile(fpfUrl);
				flyManager = new Cesium.FlyManager({
					scene: scene,
					routes: routes
				});
				if (flyManager.readyPromise) {
					Cesium.when(flyManager.readyPromise,
					function() {
						flyManager.play();
					})
				}
				
				$.each(layer,function(i, v) {

					//alert(layer[i]._name);
					

					if (v instanceof Cesium.S3MTilesLayer) {

						

						if(layer[i]._name == "建筑面"){
							//layer[i].style3D.fillForeColor.alpha = parseFloat(0);
							layer[i]._visibleDistanceMax = 20000;
						}else if(layer[i]._name == "东山口方案"){
							
							layer[i]._visibleDistanceMax = 4000;
						}else if(layer[i]._name == "酒店方案"){
							
							layer[i]._visibleDistanceMax = 2500;
						}else if(layer[i]._name == "行政中心方案"){
							
							layer[i]._visibleDistanceMax = 4000;
						}else if(layer[i]._name == "树"){
							
							layer[i]._visibleDistanceMax = 8000;
						}else if(layer[i]._name == "水面"){
							
							layer[i]._visibleDistanceMax = 3000;
						}else if(layer[i]._name == "给污雨水管网"){
							
							layer[i]._visibleDistanceMax = 500;
						}else if(layer[i]._name == "管点"){
							
							layer[i]._visibleDistanceMax = 500;
						}else if(layer[i]._name == "倾斜摄影"){
							
							layer[i]._visibleDistanceMax = 1500;
						}else if(layer[i]._name == "地形"){
							
							layer[i]._visibleDistanceMax = 7000;
						}else if(layer[i]._name == "遥感影像"){
							
							layer[i]._visibleDistanceMax = 8000;
						}
				    }

				   

				});
				
				//加载图层管理js
				APP.layerManage(layer);

				//信息属性
				var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
				handler.setInputAction(function(e) {
					$.each(layer,function(i, v) {
						if (v instanceof Cesium.S3MTilesLayer) {
							var ids = v.getSelection();
							//console.log(ids);
							if (ids.length > 0) {
								// console.log(v._name,ids);
								GlobalLayer = v;
								GlobalLayerName = v._name;
								GlobalLayerId = ids[0];
								crud.selectfind(ids[0],v._name);
								return false;
							}
						}
					});
				},
				Cesium.ScreenSpaceEventType.LEFT_CLICK);
		
				 

			});



			
		}catch(e){
			alert('渲染时发生错误，已停止渲染。');
		}

		//加载二三维一体化模块	
		//APP.twoThree(mapurl);  
	}
	return {onload: onload};
});

